package com.kecheng.action;

import com.kecheng.com.utils.WxUserHelpper;
import com.kecheng.dao.CommonDao;
import com.kecheng.moban.PageBean;
import com.kecheng.pojo.Banji;
import com.kecheng.pojo.Order;
import com.kecheng.pojo.OrderDetail;
import com.kecheng.pojo.Team;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 后台用户模块
 *
 * @author
 */

@ParentPackage("json-default")
@Action(value = "order", results = {
        @Result(name = "orderList", location = "/admin/order/orderList.jsp"),
        @Result(name = "toDetail", location = "/admin/order/detail.jsp"),
        @Result(name = "redirectList", type = "redirect", location = "order!orderList.action"),
        @Result(name = "wxOrderList", type = "json", location = "orderList"),
})
@AllowedMethods(value = {("regex:.*")})
public class OrderAction extends BaseAction {

    @Autowired
    CommonDao dao;
    private Order order;
    private List<Integer> id;
    private PageBean<Order> pageBean;
    private List<Order> orderList;

    /**
     * 订单列表
     *
     * @return
     */
    public String orderList() {
        if (order == null) {
            order = new Order();
            order.setStatus(0);
            pageBean = (PageBean<Order>) dao.findPageBean(Order.class, pageBean);
        } else {
            pageBean = (PageBean<Order>) dao.findPageBeanByObj(order, pageBean);
        }

        List<Order> orders = pageBean.getData();
        if (orders != null) {
            for (Order oo : orders) {
                Team team = new Team();
                team.setOpenid(oo.getBuyerid());
                team = (Team) dao.findOne(team, "openid");
                oo.setTeam(team);
            }
        }
        pageBean.setData(orders);
        return "orderList";
    }

    /**
     * 订单列表
     *
     * @return
     */
    public String wxOrderList() {
        order = new Order();
        order.setBuyerid(WxUserHelpper.getOpenid());
        orderList = (List<Order>) dao.find(order);
        return "wxOrderList";
    }

    /**
     * 订单详情页
     *
     * @return
     */
    public String toDetail() {
        if (order != null) {
            order = (Order) dao.findOne(order);
            Team team = new Team();
            team.setOpenid(order.getBuyerid());
            team = (Team) dao.findOne(team, "openid");
            order.setTeam(team);

            if (order.getType() == 1) {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setOrderNumber(order.getOrderNumber());
                List<OrderDetail> orderDetailList = (List<OrderDetail>) dao.find(orderDetail);
                order.setOrderDetails(orderDetailList);
            } else {
                Banji banji = new Banji();
                banji.setId(order.getBanjiId());
                banji = (Banji) dao.findOne(banji);
                order.setBanji(banji);
            }
        }
        return "toDetail";
    }

    /**
     * 删除
     *
     * @return
     */
    @Transactional(readOnly = false)
    public String delete() {
        if (order != null) dao.delete(order);
        else if (id != null) {
            for (Integer i : id) {
                order = new Order();
                order.setOrderId(i);
                dao.delete(order);
            }
        }
        return "redirectList";
    }

    public PageBean<Order> getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean<Order> pageBean) {
        this.pageBean = pageBean;
    }

    public List<Integer> getId() {
        return id;
    }

    public void setId(List<Integer> id) {
        this.id = id;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }
}
